package com.ruoyi.medical.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.medical.domain.Prescription;
import com.ruoyi.medical.domain.PrescriptionItem;
import com.ruoyi.medical.service.IPrescriptionItemService;
import com.ruoyi.medical.service.IPrescriptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 处方管理Controller
 * 
 * @author WGH
 * @date 2025-08-13
 */
@RestController
@RequestMapping("/medical/prescription")
public class PrescriptionController extends BaseController
{
    @Autowired
    private IPrescriptionService prescriptionService;
    @Autowired
    private IPrescriptionItemService prescriptionItemService;

    /**
     * 查询处方管理列表
     */
    @PreAuthorize("@ss.hasPermi('medical:prescription:list')")
    @GetMapping("/list")
    public TableDataInfo list(Prescription prescription)
    {
        startPage();
        List<Prescription> list = prescriptionService.selectPrescriptionList(prescription);
        return getDataTable(list);
    }

    /**
     * 导出处方管理列表
     */
    @PreAuthorize("@ss.hasPermi('medical:prescription:export')")
    @Log(title = "处方管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Prescription prescription)
    {
        List<Prescription> list = prescriptionService.selectPrescriptionList(prescription);
        ExcelUtil<Prescription> util = new ExcelUtil<Prescription>(Prescription.class);
        util.exportExcel(response, list, "处方管理数据");
    }

    /**
     * 获取处方管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('medical:prescription:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(prescriptionService.selectPrescriptionById(id));
    }

    /**
     * 新增处方管理
     */
    @PreAuthorize("@ss.hasPermi('medical:prescription:add')")
    @Log(title = "处方管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Prescription prescription)
    {
        // 1. 保存处方主表
        int result = prescriptionService.insertPrescription(prescription);
        if (result <= 0) {
            return AjaxResult.error("保存处方失败");
        }

        // 2. 保存处方项
        if (prescription.getPrescriptionItems() != null && !prescription.getPrescriptionItems().isEmpty()) {
            for (PrescriptionItem item : prescription.getPrescriptionItems()) {
                item.setPrescriptionId(prescription.getId()); // 设置处方ID
                prescriptionItemService.insertPrescriptionItem(item);
            }
        }

        return AjaxResult.success(prescription.getId());
    }

    /**
     * 修改处方管理
     */
    @PreAuthorize("@ss.hasPermi('medical:prescription:edit')")
    @Log(title = "处方管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Prescription prescription)
    {
        return toAjax(prescriptionService.updatePrescription(prescription));
    }

    /**
     * 删除处方管理
     */
    @PreAuthorize("@ss.hasPermi('medical:prescription:remove')")
    @Log(title = "处方管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(prescriptionService.deletePrescriptionByIds(ids));
    }
}
